<!DOCTYPE html>
<html lang="en">
<head>
    <%include("/common/header.html",{title:'设置我的资料'}){}%>
</head>
<body>
<div class="layui-form-item">
    <label class="layui-form-label">修改头像</label>
    <div class="layui-input-inline uploadHeadImage">
        <div class="layui-upload-drag" id="headImg">
            <i class="layui-icon"></i>
            <p>点击上传图片，或将图片拖拽到此处</p>
        </div>
    </div>
    <div class="layui-input-inline">
        <div class="layui-upload-list">
            <img class="layui-upload-img headImage" src="http://t.cn/RCzsdCq" id="demo1">
            <p id="demoText"></p>
        </div>
    </div>
</div>

<table class="layui-hide" id="demo" lay-filter="test"></table>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">查看</a>
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<%include("/common/foot.html"){}%>
<script type="text/javascript">
    layui.use(["jquery", "upload","table", "form", "layer", "element"], function () {
        var $ = layui.$,
            element = layui.element,
            layer = layui.layer,
            upload = layui.upload,
            form = layui.form;
            table = layui.table;

        //执行一个 table 实例
        table.render({
            elem: '#demo'
            ,id: 'listReload'
            ,toolbar: '#toolbarDemo'
            ,defaultToolbar: ['filter', 'print', 'exports']
            ,height: 420
            ,url: 'carousel/findAll.json' //数据接口
            ,cols: [[ //表头
                 {type: 'checkbox', fixed: 'left'}
                ,{field: 'title', title: '标题', width: 150, sort: true, totalRow: true}
                ,{field: 'content', title: '内容描述', width:150, sort: true}
                ,{field: 'image', title: '图片', width:150, sort: true}
                ,{field: 'createDate', title: '评分', width: 150, sort: true, totalRow: true}
                ,{fixed: 'right', width: 165, align:'center', toolbar: '#barDemo'}
            ]]
        });

        //监听头工具栏事件
        table.on('toolbar(test)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id)
                ,data = checkStatus.data; //获取选中的数据
            switch(obj.event){
                case 'add':
                    layer.msg('添加');
                    break;
                case 'update':
                    if(data.length === 0){
                        layer.msg('请选择一行');
                    } else if(data.length > 1){
                        layer.msg('只能同时编辑一个');
                    } else {
                        layer.alert('编辑 [id]：'+ checkStatus.data[0].id);
                    }
                    break;
                case 'delete':
                    if(data.length === 0){
                        layer.msg('请选择一行');
                    } else {
                        layer.msg('删除');
                    }
                    break;
            };
        });

        //拖拽上传
         upload.render({
            elem: '#headImg'
            , url: 'upload/upload.json'
            , size: 500
            , before: function (obj) {
                //预读本地文件示例，不支持ie8
                obj.preview(function (index, file, result) {
                    $('#demo1').attr('src', result); //图片链接（base64）
                });
            }
            , done: function (res) {
                //如果上传失败
                if (res.code > 0) {
                    return layer.msg('上传失败');
                }
                //上传成功
                //打印后台传回的地址: 把地址放入一个隐藏的input中, 和表单一起提交到后台, 此处略..
                /*   console.log(res.data.src);*/
                window.parent.uploadHeadImage(res.data.src);
                var demoText = $('#demoText');
                demoText.html('<span style="color: #8f8f8f;">上传成功!!!</span>');
            }
            , error: function () {
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-mini demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function () {
                    uploadInst.upload();
                });
            }
        });
        element.init();
    });
</script>
</body>
</html>